package com.navinfo.platform.trip.common.arithmetic;

import com.navinfo.platform.trip.common.arithmetic.data.CommonData;
import com.navinfo.platform.trip.common.arithmetic.data.OuterStatisticData;
import com.navinfo.platform.trip.common.arithmetic.var.VarMileageFuel;
import com.navinfo.platform.trip.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.platform.trip.common.util.ArithmeticUtils;

import java.math.BigDecimal;

/**
 * 里程、油耗计算<br/>
 * 赋值{@link CommonData#setDffFuel}公共参数的油耗差<br/>
 * 赋值{@link CommonData#setDffMileage}公共参数的里程差<br/>
 * 赋值{@link CommonData#setDffTime}公共参数的引擎工作时长<br/>
 * 输出：统计<br/>
 * @author machi、web
 * @create 2018/3/27
 */
public class Mileage extends AbstractArithmetic<VarMileageFuel> {

    @Override
    public void calculate() {
        logger.debug("===============里程、油耗算法开始===========");
        VarMileageFuel var = super.getObj();
        CommonData commonData = getCommonData();

        //当前点与上个点的里程差：当前点速度(单位:m/s)*与上一点时间差(单位:秒)
        BigDecimal speedMs = commonData.getSpeedMs();
        BigDecimal dffTimeSecond = BigDecimal.valueOf(commonData.getDffTime()).divide(BigDecimal.valueOf(1000), 2, BigDecimal.ROUND_HALF_UP);
        BigDecimal integralMileage = speedMs.multiply(dffTimeSecond);
        //设置两点间的里程差
        commonData.setDffMileage(integralMileage.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());

        //行程内积分里程(单位:M)
        double routeIntegralMileage = BigDecimal.valueOf(var.getRouteIntegralMileage()).add(integralMileage).doubleValue();
        var.setRouteIntegralMileage(routeIntegralMileage);
        //设置行程的积分里程
        commonData.setIntegralMileage(routeIntegralMileage);
        logger.debug("两点时差(单位:秒)：{},速度(M/S)：{},里程(单位:米)：{},行程内积分里程(单位:米){}", dffTimeSecond, speedMs, integralMileage, routeIntegralMileage);

        //当前点与上个点的油耗差：发动机燃油消耗率(单位:L/H)转换成ml/s * 两点时间差
        BigDecimal fuelConsumptionRateBig = commonData.getFuelConsumptionRate().divide(BigDecimal.valueOf(3.6), 2, BigDecimal.ROUND_HALF_UP);
        BigDecimal dffOilBigDecimal = fuelConsumptionRateBig.multiply(dffTimeSecond);
        //设置两点间的油耗差
        commonData.setDffFuel(dffOilBigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());

        //行程内积分油耗(单位:ml)
        double routeIntegralFuel = BigDecimal.valueOf(var.getRouteIntegralFuelConsumption()).add(dffOilBigDecimal).doubleValue();
        var.setRouteIntegralFuelConsumption(routeIntegralFuel);
        //设置行程的积分油耗
        commonData.setIntegralFuel(routeIntegralFuel);
        logger.debug("两点时差(单位:秒)：{},发动机燃油消耗率(ml/s)：{},油耗(ml)：{},行程内油耗(单位:ml) {}", dffTimeSecond, fuelConsumptionRateBig, dffOilBigDecimal.doubleValue(), routeIntegralFuel);


        //累加工作时长(单位:秒)
        double engineWorkTime = BigDecimal.valueOf(var.getEngineWorkTime()).add(dffTimeSecond).doubleValue();
        var.setEngineWorkTime(engineWorkTime);
        //设置行程的引擎工作时长
        commonData.setIntegralDuration(engineWorkTime);

        logger.debug("===============里程、油耗算法结束===========");
    }

    @Override
    public void collect(OuterStatisticData statisticData) {
        VarMileageFuel varMileageFuel = super.getObj();
        statisticData.setRouteIntegralMileage(ArithmeticUtils.mileageMToM(varMileageFuel.getRouteIntegralMileage()));
        statisticData.setRouteIntegralFuel(ArithmeticUtils.fuelMlToMl(varMileageFuel.getRouteIntegralFuelConsumption()));
        statisticData.setRouteIntegralDuration(ArithmeticUtils.timeSToS(varMileageFuel.getEngineWorkTime()));
    }
}
